package elementui.form;

import elementui.base.Button;
import elementui.base.Icon;

import java.util.ArrayList;

/**
 * javazgw@gmail.com windown 10
 * 2020/6/28 20:05
 */
public class Form {






    ArrayList<FormItem> formItems  = new ArrayList<FormItem>();

    public Form()
    {

    }

    public void init()
    {

    }

    public String getFormStr()
    {
        StringBuffer formstr = new StringBuffer();
        formstr.append("<el-form ref=\"form\" :model=\"form\" label-width=\"80px\">");
        for(FormItem fi : formItems)
        {
            formstr.append(" <el-form-item label=\""+fi.getTitle()+"\">");
            formstr.append(fi.getDefaultHmtl());
            formstr.append(" </el-form-item>");
        }

        formstr.append("<el-button type=\"primary\" @click=\"onSubmit\">立即创建</el-button>\n" +
                "    <el-button>取消</el-button>");

        formstr.append("</el-form>");


        return new String(formstr);
    }

    public String getFormDataScript()
    {
       /* datastr.append("   data() {\n" +
                "      return {\n" +
                "        form: {\n" +
                " checked: true ,\n"+
                "          name: '',\n" +
                "          region: '',\n" +
                "          date1: '',\n" +
                "          date2: '',\n" +
                "           input:'' ,\n"+
                "          delivery: false,\n" +
                "          type: [],\n" +
                "          resource: '',\n" +
                "          desc: ''\n" +
                "        }\n" +
                "      }\n" +
                "    },");*/
        StringBuffer datastr = new StringBuffer();
        datastr.append("data() {\n");
        datastr.append("return {\n");
        datastr.append("form:  {\n");
        for(FormItem fi : formItems)
        {

            datastr.append(fi.getData());
            datastr.append(",\n");

        }
        datastr.append("}");
        datastr.append("}");
        datastr.append("}");

        return new String(datastr);
    }
    public String getFormMethodScript()
    {
        StringBuffer scripts = new StringBuffer();
        scripts.append(" methods: { ");
        for(FormItem fi : formItems)
        {
            if(fi.getDefaultScript()!=null) {
                scripts.append(fi.getDefaultScript());
                scripts.append(",");
            }
        }
        scripts.append("}");
        return new String(scripts);
    }
    public void addFormItem(FormItem formitem)
    {
            formItems.add(formitem);
    }


    public  static void  main (String[] argc)
    {

        String[][] items = new String[][]{
                {"bcode","姓名","input"},
                {"sex1","性别","checkbox"},
                {"birddate","日期","datePicker"},
                {"sex","性别2","Radio"},
                {"age","年龄","InputNumber"},
                {"like","爱好","Select"},
                {"auto","喜欢吃的","autocomplate"},
                {"fileset","资料","Upload"}



        };
        Form form = new Form();
        for(int i=0;i<items.length;i++)
        {
            FormItem formItem;
            switch (items[i][2].toLowerCase())
            {
                case "input":
                    formItem  = new Input();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "checkbox":
                    formItem = new Checkbox();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "datepicker":
                    formItem = new DatePicker();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "radio":
                    formItem = new Radio();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "inputnumber":
                    formItem = new InputNumber();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "select":
                    formItem = new Select();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData("options: [{\n" +
                            "          value: '选项1',\n" +
                            "          label: '黄金糕'\n" +
                            "        }, {\n" +
                            "          value: '选项2',\n" +
                            "          label: '双皮奶'\n" +
                            "        }, {\n" +
                            "          value: '选项3',\n" +
                            "          label: '蚵仔煎'\n" +
                            "        }, {\n" +
                            "          value: '选项4',\n" +
                            "          label: '龙须面'\n" +
                            "        }, {\n" +
                            "          value: '选项5',\n" +
                            "          label: '北京烤鸭'\n" +
                            "        }],\n" +
                            items[i][0] + "    : ''");
                    break;
                case "autocomplate":
                    formItem = new Autocomplate();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData("options: [{\n" +
                            "          value: '选项1',\n" +
                            "          label: '黄金糕'\n" +
                            "        }, {\n" +
                            "          value: '选项2',\n" +
                            "          label: '双皮奶'\n" +
                            "        }, {\n" +
                            "          value: '选项3',\n" +
                            "          label: '蚵仔煎'\n" +
                            "        }, {\n" +
                            "          value: '选项4',\n" +
                            "          label: '龙须面'\n" +
                            "        }, {\n" +
                            "          value: '选项5',\n" +
                            "          label: '北京烤鸭'\n" +
                            "        }],\n" +
                            items[i][0] + "    : ''");
                    break;
                case "upload":
                    formItem = new Upload();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+" : [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]");
                    ((Upload)formItem).setAction("http://192.168.1.21:58080/audit/up");
                    break;
                default:
                    formItem  = new Input();
                    formItem.setVmodel("form."+items[i][0]);
            }

            //formItem.setVmodel(items[i][0]);

            form.addFormItem(formItem);
        }

/*
        Form form = new Form();
        for(int i = 0 ; i< 2 ;i++)
        {
            Input input = new Input();
            input.setVmodel("form.input");
            form.addFormItem(input);

        }

        Autocomplete autocomplete = new Autocomplete();
        autocomplete.setVmodel("form.state1");
        Checkbox checkbox =new Checkbox();
        checkbox.setVmodel("form.checked");
        DatePicker datePicker = new DatePicker();
        datePicker.setVmodel("form.value1");
        InputNumber inputNumber = new InputNumber();
        inputNumber.setVmodel("form.num");
        Radio r = new Radio();
        r.setVmodel("form.radio");
        Select select = new Select();
        select.setVmodel("form.value");
        Upload upload = new Upload();
        upload.setVmodel("form.fileList");
        upload.setAction("http://192.168.1.21:58080/audit/up");

        form.addFormItem(autocomplete);
        form.addFormItem(checkbox);
        form.addFormItem(datePicker);
        form.addFormItem(inputNumber);
        form.addFormItem(r);
        form.addFormItem(select);
        form.addFormItem(upload);


 */

        System.out.println("--------------------------------------");
        String html = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <!-- import CSS -->\n" +
                "    <link rel=\"stylesheet\" href=\"https://unpkg.com/element-ui/lib/theme-chalk/index.css\">\n" +
                "</head>\n" +
                "<body>"+
                "<div id=\"app\">"
                ;
        String endhtml = "";
        System.out.println(html);
        System.out.println(form.getFormStr());

        System.out.println("</div>\n" +
                "</body>" +
                "<!-- import Vue before Element -->\n" +
                "<script src=\"https://unpkg.com/vue/dist/vue.js\"></script>\n" +
                "<!-- import JavaScript -->\n" +
                "<script src=\"https://unpkg.com/element-ui/lib/index.js\"></script>\n" +
                "<script>\n" +
                "    new Vue({\n" +
                "        el: '#app',\n");
        System.out.println(form.getFormDataScript());
        System.out.println(",");
        System.out.println(form.getFormMethodScript());
        System.out.println(",");
        System.out.println("  mounted() {\n" +
                "                console.log(\"mounted\")\n" +
                "                this.restaurants = this.loadAll();\n" +
                "            }\n" +
                "\n" +
                "\n" +
                "\n" +
                "        })");
        System.out.println("\n" +
                "</script>\n" +
                "</html>");



        System.out.println("--------------------------------------");
        Button button = new Button();
        button.setIcon(Icon.el_icon_star_on);
        button.setAutofocus(true);
        button.setCircle(true);
        System.out.println(button.getHtml());
    }

}
